package player;

public class pingChecker extends Thread{
    public int pingCnt = 3;
    public boolean waitFlag = false;
	
    private static volatile pingChecker instance = null;
    
    public static pingChecker getInstance() {
        if (instance == null){
            synchronized (pingChecker.class){
                    if (instance == null){
                        instance = new pingChecker();
                    }
        }}
        return instance;
    }
    
	public pingChecker(){}
    
    public synchronized void run(){	
    	try {
			Thread.sleep(2000);
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
    	
    	while(pingCnt > 0){
        	if(waitFlag){
        		System.err.println("Players matched. Delaying ping checker.");
        		try {
    				this.wait();
    			} 
        		catch (InterruptedException e) {
    				System.err.println("Cannot suspend pingChecker.\n"+e.getMessage());
        			e.printStackTrace();
    			}
        	}
    		try {
				Thread.sleep(1000);
			}
    		catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.pingCnt--;
    	}
    	
		System.err.println("Exiting pingChecker after user removal.");
    	player.getInstance().pingerFailFlag = true;
    	return;
    	}
		
}
